﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Handrise.CDCable.Mobile.Business;
using HandRise.MBHOP.Controls;

namespace Handrise.CDCable.Mobile.UI
{
    public partial class FrmTemperatureEdit : FrmBase
    {
        private Temprature temperature = null;
        private int editType = 0;  //0:新增 1:修改
        private Device device = null;
        private TempratureService tempratureService = new TempratureService();


        public FrmTemperatureEdit()
        {
            InitializeComponent();
            ttlbrhead.Text = "测温管理";
            ilstbcup.Images.Add(ResourceImage.确定01);
            ilstbcup.Images.Add(ResourceImage.键盘01);
            ilstbcup.Images.Add(ResourceImage.返回01);
            ilstbcdown.Images.Add(ResourceImage.确定02);
            ilstbcdown.Images.Add(ResourceImage.键盘02);
            ilstbcdown.Images.Add(ResourceImage.返回02);
        }

        private void FrmTemperatureEdit_Load(object sender, EventArgs e)
        {
            if (IsReturn)
                LoadCacheData();
            else
                LoadData();

            tbTitle.Text = editType == 0 ? "新增测温信息" : "修改测温信息";

            if (temperature == null)
            {
                temperature = new Temprature();
                temperature.TempratureID = Guid.NewGuid().ToString();
                temperature.CollectDateTime = DateTime.Now;
                temperature.ReportState = "未上报";
                temperature.UserName = AppLogic.LoginUser.UserName;
                temperature.Version = 0;
            }

            if (device != null)
            {
                temperature.DeviceID = device.DeviceID;
                temperature.DeviceName = device.Name;
            }


            tbDeviceName.Text = temperature.DeviceName;
            if (temperature.TempratureValue.HasValue)
                tbTempratureValue.Text = temperature.TempratureValue.Value.ToString();
            lbCollectDateTime.Text = temperature.CollectDateTime.ToString();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            if (PriorForm == "FrmQueryDevice" || PriorForm == "FrmSelectDevice")
            {
                //设备选择进入
                device = (Device)Parameter[0];
                object[] oldParameters = (object[])Parameter[1];
                editType = (int)oldParameters[1];
                temperature = (Temprature)oldParameters[2];
            }
            else
            {
                editType = (int)Parameter[0];
                temperature = (Temprature)Parameter[1];
                //device = (Device)Parameter[2];
            }
        }

        /// <summary>
        /// 加载缓存数据
        /// </summary>
        public void LoadCacheData()
        {
            editType = (int)Caches[0];
            temperature = (Temprature)Caches[1];
            //device = (Device)Caches[2];
        }

        private void bcOpreate_OnButtonClick(object sender, int buttonIndex)
        {
            switch (buttonIndex)
            {
                case 0: //保存
                    SaveTemperature();
                   break;
                case 1:
                    InputMethod.SHFullScreen(this.Handle, 4);
                    InputMethod.SipShowIM(1);
                    break;
                case 2:
                    FormManage.GotoNext("FrmTemperatureList", null, null, true);
                    break;
            }
        }

        private void btnDeviceName_Click(object sender, EventArgs e)
        {
            FormManage.GotoNext("FrmQueryDevice", new object[] { "测温设备选择", new object[] { "FrmTemperatureEdit", editType, temperature } }, new object[] { editType, temperature, device }, false);
        }

        private bool SaveTemperature()
        {
            bool re = false;

            if (temperature.DeviceName == "")
            {
                mssdlgConfirm.ShowDialog(this, "设备名称不能为空", DialogType.Confirm, FormManage.form_Deactivate);

                return false;
            }

            double value;
            try
            {
                value = double.Parse(tbTempratureValue.Text);
                temperature.TempratureValue = value;
            }
            catch (Exception e)
            {
                mssdlgConfirm.ShowDialog(this, "测试温度输入错误", DialogType.Confirm, FormManage.form_Deactivate);

                return false;
            }


            //保存
            switch (editType)
            {
                case 0:   //新增
                    //设置标题
                    temperature.Title = temperature.TempratureValue.Value.ToString() + "℃ (" + temperature.DeviceName + ")";
                    re = tempratureService.AddTemperature(temperature);
                    break;
                case 1:   //编辑
                    re = tempratureService.UpdateTemperature(temperature);
                    break;
                default:
                    break;
            }

            if (re)
            {
                editType = 1;
                mssdlgConfirm.ShowDialog(this, "保存成功", DialogType.Confirm, FormManage.form_Deactivate);
            }
            else
            {
                mssdlgConfirm.ShowDialog(this, "保存失败", DialogType.Confirm, FormManage.form_Deactivate);
            }
            return re;
        }


    }
}